import re
import time

from rest_framework import serializers

from article_app.models import Article, Comment, CommentRelation, Message
from user_app.models import User
from utils.encode_decode import my_encode
from utils.encode_decode import ShortLink
from utils.time_zom import time2chinese

# 猴子补丁的写法
my_encode = ShortLink().encrypt

class ArticleSimpleSerializer(serializers.ModelSerializer):

    nick_name = serializers.SerializerMethodField()
    aid = serializers.SerializerMethodField()
    add_time = serializers.SerializerMethodField()
    change_time = serializers.SerializerMethodField()
    # content_text = serializers.SerializerMethodField()
    full = serializers.SerializerMethodField()
    user_image = serializers.SerializerMethodField()
    status = serializers.SerializerMethodField()
    tags = serializers.SerializerMethodField()
    user_tags = serializers.SerializerMethodField()
    is_public = serializers.SerializerMethodField()
    user_level = serializers.SerializerMethodField()

    class Meta:
        model = Article
        fields = ('user_level', 'aid', 'title', "nick_name", "add_time", "change_time", "head_avatar", "mode", "full", "user_image", "view_no", "agree_no", "comment_no", "star_no", "status", "is_check", "word", "tags", "user_tags", "is_public", "is_recommend")

    def get_is_public(self, obj):
        return True if obj.is_public else False

    def get_tags(self, obj):
        return obj.tags.split('|')

    def get_user_level(self, obj):
        level = obj.u_id.level
        text = f"V{level}"
        return text

    def get_user_tags(self, obj):
        return obj.u_id.tags.split('|')

    def get_nick_name(self, obj):
        return obj.u_id.nick_name + ": " + obj.u_id.info

    def get_user_image(self, obj):
        return obj.u_id.avatar  # 关联查询的结果  obj.u_id为一个User对象

    def get_full(self, obj):
        cache = obj.content_text
        return True if len(cache) <= 600 else False

    # def get_content_text(self, obj):
    #     cache = obj.content_text
    #     return cache if len(cache) <= 600 else cache[:600] + "..."

    def get_aid(self, obj):
        my_encode = ShortLink().encrypt
        return my_encode(obj.id)

    def get_status(self, obj):
        if obj.is_check == 1:
            word = "已发布"
        elif obj.is_check == -1:
            word = "已驳回"
        else:
            word = "待审核"
        return word

    def get_add_time(self, obj):
        time_ = obj.add_time
        day_time = time2chinese(time_)
        return day_time

    def get_change_time(self, obj):
        time_ = obj.change_time
        day_time = time2chinese(time_)
        return day_time


class ArticleDetailSerializer(serializers.ModelSerializer):
    nick_name = serializers.SerializerMethodField()
    aid = serializers.SerializerMethodField()
    u_id = serializers.SerializerMethodField()
    add_time = serializers.SerializerMethodField()
    change_time = serializers.SerializerMethodField()
    text = serializers.SerializerMethodField()
    tags = serializers.SerializerMethodField()

    class Meta:
        model = Article
        fields = ('aid', 'title', 'text', 'add_time', 'u_id', "nick_name", "change_time",  "mode", "view_no", "agree_no", "comment_no", "star_no", "is_check", "head_avatar", "tags")

    def get_tags(self, obj):
        return obj.tags.split('|')

    def get_nick_name(self, obj):
        return obj.u_id.nick_name

    def get_text(self, obj):
        return re.sub("图像小部件</div>", "</div>", obj.text)

    def get_aid(self, obj):
        return my_encode(obj.id)
    
    def get_u_id(self, obj):
        return my_encode(obj.u_id.id)

    def get_add_time(self, obj):
        time_ = obj.add_time
        day_time = time2chinese(time_)
        return day_time
    
    def get_change_time(self, obj):
        time_ = obj.change_time
        day_time = time2chinese(time_)
        return day_time


class CommentMiniSerializer(serializers.ModelSerializer):

    nick_name = serializers.SerializerMethodField()
    cid = serializers.SerializerMethodField()
    content = serializers.SerializerMethodField()

    class Meta:
        model = Comment
        fields = ('cid', "nick_name", "content")

    def get_cid(self, obj):
        return my_encode(obj.id)

    def get_content(self, obj):
        return '评论已被删除' if obj.is_delete else obj.content

    def get_nick_name(self, obj):
        return obj.from_uid.nick_name


class CommentSimpleSerializer(serializers.ModelSerializer):

    nick_name = serializers.SerializerMethodField()
    add_time = serializers.SerializerMethodField()
    user_image = serializers.SerializerMethodField()
    cid = serializers.SerializerMethodField()
    rule = serializers.SerializerMethodField()
    reply = serializers.SerializerMethodField()

    class Meta:
        model = Comment
        fields = ('cid', 'content', "nick_name", "add_time", "user_image", "rule", "reply", "level")

    def get_reply(self, obj):
        """获取回复内容"""
        if obj.level == 2:
            # 从关系库获取当前评论的父级id
            parent = CommentMiniSerializer(CommentRelation.objects.get(child_id=obj.id).parent_id)
            return parent.data
        else:
            return {}

    def get_rule(self, obj):
        """获取角色"""
        return "博主" if obj.article_id.u_id == obj.from_uid else "路过"

    def get_cid(self, obj):
        return my_encode(obj.id)

    def get_nick_name(self, obj):
        return obj.from_uid.nick_name

    def get_user_image(self, obj):
        return obj.from_uid.avatar  # 关联查询的结果  obj.u_id为一个User对象

    def get_add_time(self, obj):
        time_ = obj.add_time
        day_time = time2chinese(time_)
        return day_time


class CommentDetailSerializer(serializers.ModelSerializer):

    parent_id = CommentMiniSerializer(label='被回复对象')
    child_id = CommentSimpleSerializer()

    class Meta:
        model = CommentRelation
        fields = ("parent_id", "child_id")

    def get_content(self, obj):
        return obj.child_id.content

    def get_to_user(self, obj):
        """获取回复对象昵称"""
        return obj.parent_id.from_uid.nick_name

    def get_rule(self, obj):
        """获取角色"""
        return "作者" if obj.child_id.article_id.u_id == obj.child_id.from_uid else "围观"

    def get_cid(self, obj):
        return my_encode(obj.id)

    def get_nick_name(self, obj):
        return obj.child_id.from_uid.nick_name

    def get_user_image(self, obj):
        return obj.child_id.article_id.u_id.avatar  # 关联查询的结果  obj.u_id为一个User对象

    def get_add_time(self, obj):
        time_ = obj.child_id.add_time
        day_time = time2chinese(time_)
        return day_time





class AdminArticleSimpleSerializer(serializers.ModelSerializer):
    """展示给审核员看的简略信息"""

    aid = serializers.SerializerMethodField()
    add_time = serializers.SerializerMethodField()
    change_time = serializers.SerializerMethodField()

    class Meta:
        model = Article
        fields = ('aid', 'title', "add_time", "change_time", "mode")


    def get_aid(self, obj):
        my_encode = ShortLink().encrypt
        return my_encode(obj.id)

    def get_add_time(self, obj):
        time_ = obj.add_time
        day_time = time2chinese(time_)
        return day_time

    def get_change_time(self, obj):
        time_ = obj.change_time
        day_time = time2chinese(time_)
        return day_time



class AdminArticleDetailSerializer(serializers.ModelSerializer):
    """页面详情"""

    aid = serializers.SerializerMethodField()
    text = serializers.SerializerMethodField()
    tags = serializers.SerializerMethodField()

    class Meta:
        model = Article
        fields = ('aid', 'title', 'text', "head_avatar", "tags")

    def get_tags(self, obj):
        return obj.tags.split('|')

    def get_text(self, obj):
        return re.sub("图像小部件</div>", "</div>", obj.text)

    def get_aid(self, obj):
        return my_encode(obj.id)

class MessageSimpleSerializer(serializers.ModelSerializer):
    
    mid = serializers.SerializerMethodField()
    from_ = serializers.SerializerMethodField()
    from_id = serializers.SerializerMethodField()
    link = serializers.SerializerMethodField()
    link_com = serializers.SerializerMethodField()
    link_name = serializers.SerializerMethodField()
    mode = serializers.SerializerMethodField()
    add_time = serializers.SerializerMethodField()

    class Meta:
        model = Message
        fields = ('mid', 'from_id', 'from_', 'info', 'need_reply', 'link', 'link_com', 'link_name', 'mode', 'add_time', 'message_type')

    def get_from_(self, obj):
        if obj.from_id == 0:
            return "系统"
        return User.objects.get(pk=obj.from_id).nick_name

    def get_mode(self, obj):
        if obj.link and obj.link != "0":
            return Article.objects.get(pk=obj.link).mode
        else:
            return 0

    def get_link_name(self, obj):
        if obj.link_com and obj.link_com != "0":
            return Article.objects.get(pk=obj.link).title
        else:
            return ""
    
    def get_add_time(self, obj):
        time_ = obj.add_time
        day_time = time2chinese(time_)
        return day_time

    def get_mid(self, obj):
        return my_encode(obj.id)

    def get_from_id(self, obj):
        return my_encode(obj.from_id)

    def get_link(self, obj):
        return my_encode(obj.link)
    
    def get_link_com(self, obj):
        return my_encode(obj.link_com)
